import { unInstance } from '@/service'
import { useAuthStore } from '@/store'

interface RESPONSE {
  status: string
  message: string
  data: any
}

export default {
  /* 获取 OpenID */
  getOpenID(code: string) {
    return unInstance.post<RESPONSE>(`/auth/getOpenID`, {
      code,
      source: 'MT_MP',
    })
  },
  /* 请求发送短信验证码 */
  sendSMS(phoneNumber: string) {
    const { openid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/sendSMS`, {
      openid: openid.value,
      phoneNumber,
      source: 'MT_MP',
    })
  },
  /* 验证短信验证码并颁发 Token */
  verifyAndLogin(phoneNumber: string, verificationCode: string) {
    const { openid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/verifyAndLogin`, {
      openid: openid.value,
      phoneNumber,
      verificationCode,
      source: 'MT_MP',
    })
  },
  /* 小微商户入驻申请信息获取 */
  getSmallMerchantInfo(applyId: string = '') {
    const { sessionid } = storeToRefs(useAuthStore())
    if (applyId) {
      return unInstance.post<RESPONSE>(`/apply/getSmallMerchantInfo`, {
        sessionid: sessionid.value,
        applyId,
      })
    }
    else {
      return unInstance.post<RESPONSE>(`/apply/getSmallMerchantInfo`, {
        sessionid: sessionid.value,
      })
    }
  },
  /* 小微商户保存入驻信息 */
  saveStepSmallInfo(data: any) {
    return unInstance.post<RESPONSE>(`/apply/saveStepSmallInfo`, data)
  },
  /* 小微商户提交入驻信息 */
  submitAllSmallInfo(data: any) {
    return unInstance.post<RESPONSE>(`/apply/submitAllSmallInfo`, data)
  },
  /* 商户入驻申请信息获取 */
  getMerchantInfo(applyId: string = '') {
    const { sessionid } = storeToRefs(useAuthStore())
    if (applyId) {
      return unInstance.post<RESPONSE>(`/apply/getMerchantInfo`, {
        sessionid: sessionid.value,
        applyId,
      })
    }
    else {
      return unInstance.post<RESPONSE>(`/apply/getMerchantInfo`, {
        sessionid: sessionid.value,
      })
    }
  },
  /* 商户保存入驻信息 */
  saveStepInfo(data: any) {
    return unInstance.post<RESPONSE>(`/apply/saveStepInfo`, data)
  },
  /* 商户提交入驻信息 */
  submitAllInfo(data: any) {
    return unInstance.post<RESPONSE>(`/apply/submitAllInfo`, data)
  },
  /* 账户查询 */
  accountQuery(data: any) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/account/query`, Object.assign(data, {
      sessionid: sessionid.value,
    }))
  },
  /* 商户入驻进度查询 */
  progressQuery(data: any) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/progress/query`, Object.assign(data, {
      sessionid: sessionid.value
    }))
  },
  /* 商户入驻信息删除 */
  delInfo(applyId: string) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/apply/delInfo`, {
      sessionid: sessionid.value,
      applyId,
    })
  },
  /* 获取客户经理姓名 */
  getManagerName(managerNo: string) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/manager/getManagerName`, {
      sessionid: sessionid.value,
      managerNo,
    })
  },
  /* 获取地址信息 */
  getAddress(latitude: string, longitude: string) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/location/getAddress`, {
      sessionid: sessionid.value,
      latitude,
      longitude,
    })
  },
  /* 省市区级联查询 */
  getAddressCascade(applyId: string, parentCode: string) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/apply/getAddressCascade`, {
      sessionid: sessionid.value,
      applyId, 
      parentCode
    })
  },
  /* 获取经纬度信息 */
  getTude(address: string) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/location/getTude`, {
      sessionid: sessionid.value,
      address
    })
  }
}
